<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="属性名" prop="attrName">
      <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
    </el-form-item>
    <el-form-item label="分类" prop="catIdPath">
      <!-- <el-input v-model="dataForm.catId" placeholder="分类id"></el-input> -->
      <el-cascader
        v-model="dataForm.catIdPath"
        :options="categorys"
        :props="cascaderProps"
        >
      </el-cascader>
    </el-form-item>
    <el-form-item label="输入类型" prop="typeId">
        <el-select v-model="dataForm.typeId" filterable placeholder="请选择输入类型">
          <el-option
            v-for="item in inputType"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeId">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="检索字段" prop="searchType">
      <el-switch
        v-model="dataForm.searchType" :active-value="1" :inactive-value="0"
        active-color="#13ce66"
        inactive-color="#ff4949">
      </el-switch>
    </el-form-item>
    <el-form-item label="是否显示" prop="showStatus">
      <el-switch
        v-model="dataForm.showStatus" :active-value="1" :inactive-value="0"
        active-color="#13ce66"
        inactive-color="#ff4949">
      </el-switch>
    </el-form-item>
    <!-- <el-form-item label="可选值列表[用分号分隔]" prop="valueSelect">
      <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
    </el-form-item> -->
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" :max="9"></el-input-number>
    </el-form-item>
    <el-form-item label="属性图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props:{
      catIdPath: Array,
    },
    data () {
      return {
        categorys: [],
        cascaderProps: {
          label: "catName",
          value: "catId",
          children: "children",
        },
        inputType:[
          {typeId:1, typeName:'input'},
          {typeId:2, typeName:'select'},
        ],

        visible: false,
        dataForm: {
          attrId: 0,
          attrName: '',
          catId: 0,
          searchType: '',
          typeId: '',
          showStatus:'',
          valueSelect: '',
          sort: 0,
          icon: '',
          catIdPath: this.catIdPath,
        },
        dataRule: {
          attrName: [
            { required: true, message: '属性名不能为空', trigger: 'blur' }
          ],
          catIdPath: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          searchType: [
            { required: true, message: '检索字段不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '输入种类不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '是否显示不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    created(){
      // this.getCategorys();
    },
    methods: {
      // 获取 所有种类【其实可以不获取，这里获取是更方便修改种类】
      getCategorys(){
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl("/admin/info/category/list/tree"),
          method: "get",
        }).then(({ data }) => {
          // console.log("成功获取到数据");
          console.log("从后端获取数据成功：", data);
          this.categorys = data.data;
        });
      },
      // 获取 输入类型
      getInputType(){
        this.$http({
          url: this.$http.adornUrl("/admin/info/input/type/list/all/show"),
          method: "post",
        }).then(({ data }) => {
          this.$message({
            type: "success",
            message: "获取输入类型成功!",
          });
          this.inputType = data.list;
          
        });
      },

      init (id) {
        this.dataForm.attrId = id || 0
        // console.log("新增or修改时id",id);
        this.getCategorys(); // 获取 信息种类
        // if(typeof(id) == "undefined"){ console.log("没有id，是新增")
        // }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.catIdPath = this.catIdPath;
          this.dataForm.typeId = '';
          // this.dataForm.sort = 0
          this.getInputType();
          if (this.dataForm.attrId) { // 如果是 update，则从后端 再获取一次数据
            this.$http({
              url: this.$http.adornUrl(`/admin/info/attr/info/${this.dataForm.attrId}`),
              method: 'get',
            }).then(({data}) => {
              if (data && data.code === 0) {
                // 填充表单
                console.log("后台获取到的填充数据", data.infoAttr)
                var _attr = data.infoAttr;
                this.dataForm.attrName = _attr.attrName
                this.dataForm.catId = _attr.catId
                this.dataForm.searchType = _attr.searchType
                this.dataForm.typeId = _attr.typeId
                this.dataForm.showStatus = _attr.showStatus
                // this.dataForm.valueSelect = _attr.valueSelect.split(";")
                this.dataForm.sort = _attr.sort
                this.dataForm.icon = _attr.icon
                console.log("catIdPaht", this.catIdPath)
                // 查出 catIdPath 的值
                // this.dataForm.catIdPath = _attr.catIdPath
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.dataForm.catId = this.dataForm.catIdPath[this.dataForm.catIdPath.length-1];
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/info/attr/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrId': this.dataForm.attrId || undefined,
                'attrName': this.dataForm.attrName,
                'catId': this.dataForm.catId,
                'searchType': this.dataForm.searchType,
                'typeId': this.dataForm.typeId,
                'showStatus': this.dataForm.showStatus,
                // 'valueSelect': this.dataForm.valueSelect.join(";"),
                'sort': this.dataForm.sort,
                'icon': this.dataForm.icon
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                // this.dataForm.catIdPath = []
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
